package com.zgg.myjwt.controller;

import com.alibaba.fastjson.JSONObject;
import com.zgg.myjwt.annotation.CheckToken;
import com.zgg.myjwt.annotation.LoginToken;
import com.zgg.myjwt.pojo.SysUser;
import com.zgg.myjwt.untils.JwtUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 测试用的例子
 * @author ant_Zhou
 * @email 1641583112@qq.com
 * @create 2020-05-15 22:52
 */
@RestController
@RequestMapping("/api")
public class SysUserController {

    //登录
    @PostMapping("/login")
    @LoginToken
    public Object login(@RequestBody @Valid SysUser user) {
        System.out.println("---------------"+user);
        JSONObject jsonObject = new JSONObject();
        if (!StringUtils.equals("111",user.getAccount())) {
            jsonObject.put("message", "登录失败,用户不存在");
            return jsonObject;
        } else {
            if (!StringUtils.equals("abc",user.getPassword())) {
                jsonObject.put("message", "登录失败,密码错误");
                return jsonObject;
            } else {
                user.setUserId((long)2);
                String token = JwtUtil.createJWT(6000000, user);
                jsonObject.put("token", token);
                jsonObject.put("user", user);
                return jsonObject;
            }
        }
    }

    //查看个人信息
    @CheckToken
    @GetMapping("/getMessage")
    public String getMessage() {
        return "你已通过验证";
    }
}
